﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Controllers;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace Site.Controllers
{
    public class ServiceController : BaseController
    {
        public ActionResult Manager()
        {
            return View(new ManagerModel { Title = "Service manager", AttachTinyMce = true });
        }
        public ActionResult Index()
        {
            return View();
        }

        [AjaxOrChildAction]
        public ActionResult List(int? page)
        {
            int TotalRecord = 0;
            var ListNew = RsDataManager.GetList<ServiceInfo>("ServiceSearch", page ?? 1, 10, ref TotalRecord);
            ViewBag.TotalRecord = TotalRecord;
            return PartialView(ListNew);
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Create()
        {
            return PartialView();
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(ServiceInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Description))
            {
                return AjaxMessage(Message.DescriptionEmpty);
            }
            if (string.IsNullOrWhiteSpace(info.Content))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (ModelState.IsValid)
            {
                info.Content = HtmlDecode(info.Content);
                info.Description = HtmlDecode(info.Description);                
                if (RsDataManager.Save("ServiceSave", info)>0)
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id)
        {
            return PartialView(RsDataManager.GetInfo<ServiceInfo>("ServiceGetInfo", "ServiceID", id));
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(ServiceInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Description))
            {
                return AjaxMessage(Message.DescriptionEmpty);
            }
            if (string.IsNullOrWhiteSpace(info.Content))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (ModelState.IsValid)
            {
                info.Content = HtmlDecode(info.Content);
                info.Description = HtmlDecode(info.Description);                
                if (RsDataManager.Save("ServiceSave", info)>0)
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int[] id)
        {
            if (id != null)
            {
                int count = id.Count();
                foreach (int i in id)
                {
                    ServiceInfo info = RsDataManager.GetInfo<ServiceInfo>("ServiceGetInfo", "ServiceID", i);
                    if (RsDataManager.Delete("ServiceDelete", "ServiceID", i))
                    {                        
                        count--;
                    }
                }
                if (count > 0)
                {
                    AddMessage("Cannot delete " + count + " records");
                }
                else
                {
                    AddMessage(Message.DeleteSuccess);
                }
            }
            return RedirectToAction("List");
        }
        [HttpGet]
        public ActionResult Details(int id)
        {
            return View(RsDataManager.GetInfo<ServiceInfo>("ServiceGetInfo", "ServiceID", id));
        }

        [HttpGet]
        public ActionResult ListService(int? page)
        {
            int TotalRecord = 0;
            var ListNew = RsDataManager.GetList<ServiceInfo>("ServiceSearch", page ?? 1, 10, ref TotalRecord);
            ViewBag.TotalRecord = TotalRecord;
            return PartialView(ListNew);
        }

        [HttpGet]
        public ActionResult ListLeft()
        {
            return PartialView(RsDataManager.GetList<ServiceInfo>("ServiceSearch"));
        }
    }
}
